<?php

namespace app\comm\service;
/**
 * 微信服务层
 */
class WeixinService{
/*******************************************************************/	
	/**
	 * 微信发送模板消息
	 * @return [type] [description] ，可用
	 */
	public function sendmsg(array $arry ){
		$token = &load_wechat('Menu');
		$accesstoken = $token->getAccessToken();
		$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$accesstoken;
		$file=$this->HttpPost($url,$this->TempAtp($arry));
		return $file;
	}

	/**
	 * post提交数据 微信数据
	 * @param [type] $url [description]
	 * @param [type] $par [description]
	 */
	public function HttpPost($url,$par){
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $url); 
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $par);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
		$info = curl_exec($ch);
		if (curl_errno($ch)) {
		    echo 'Errno'.curl_error($ch);
		}
		curl_close($ch);
		return $info;
	}

	/**
	 * 返回消息模板的json
	 */
	public function TempAtp(array $data){
		if($data['type']==1){
			$data=[
			  	"touser"=>$data['openid'],
			  	"template_id"=>$data['template_id'],
			  	"url"       =>$data['url'],
			  	"topcolor"  =>"#FF0000",
			  	"data"      =>$data['count'],
			  ];
		}
		return json_encode($data);
	}
/*******************************************************************/

	
}

